import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import("@/views/LoginPage.vue")
    },
    {
      path: '/',
      component: () => import("@/views/LayoutPage.vue"),
      redirect:'/article/manage',
      children: [
        {
          path: '/article/manage',
          component:()=>import("@/views/ArticleManage.vue")
        },
        {
          path: '/article/channel',
          component:()=>import("@/views/ArticleChannel.vue")
        },
        {
          path: '/user/profile',
          component:()=>import("@/views/UserProfile.vue")
        },
        {
          path: '/user/password',
          component:()=>import("@/views/UserPassword.vue")
        },
        {
          path: '/user/avatar',
          component:()=>import("@/views/UserAvatar.vue")
        },
      ]
    },
    
  ]
})

//登录访问的拦截
router.beforeEach((to) => {
  const userStore = useUserStore()
  if (!userStore.token && to.path !== '/login')
    return '/login'
})

export default router
